import { getPersonnel, getSelfPersonnel } from "@/api/leaveApplication";
export default {
  namespaced: true,
  state: {
    personList: [],
    personSelfList: [],
    personListGroup: [],
  },
  mutations: {
    SET_PERSON_GROUP_LIST(state, data) {
      state.personListGroup = data;
    },
    SET_PERSON_LIST(state, data) {
      state.personList = data;
    },
    SET_PERSON_SELF_LIST(state, data) {
      state.personSelfList = data;
    },
  },
  actions: {
    async getPersonGroup({ commit, state }) {
      if (state.personListGroup.length === 0) {
        const { code, data } = await getPersonnel({ showRole: 1 });
        if (code === 200) {
          data.forEach((item) => {
            item.name = item.orgName;
            item.personId = item.orgId;
          });
          commit("SET_PERSON_GROUP_LIST", data);
        }
      }
    },
    async getPerson({ commit, state }) {
      if (state.personList.length === 0) {
        const { code, data } = await getPersonnel();
        if (code === 200) {
          data.forEach((item) => {
            item.name = item.orgName;
            item.personId = item.orgId;
          });
          commit("SET_PERSON_LIST", data);
        }
      }
    },
    async getSlefPerson({ commit, state }) {
      if (state.personSelfList.length === 0) {
        const { code, data } = await getSelfPersonnel();
        if (code === 200) {
          commit("SET_PERSON_SELF_LIST", data.cimContactsVOList);
        }
      }
    },
  },
};
